(function(){
    "use strict";
    angular.module('app')
    .controller('DetailCtrl', ['$rootScope', '$scope','$state','httpLoad','$stateParams','LANGUAGE','eChartLineFactory','$interval',
	    function($rootScope, $scope,$state,httpLoad,$stateParams,LANGUAGE,eChartLineFactory,$interval) {
		/* 变量声明
	    ---------------------------------------*/
		var flag = $stateParams.flag,
		ctime =  new Date().getTime(),
		oneDay = 2000,
		nowtime,
		cupChart={old:[]},
	    netChart={old:[]},
	    netOutChart={old:[]},
	    memoryChart={old:[]},
	    currentObj={},
	    network;
		
		/* scope变量声明
	    ---------------------------------------*/
		$rootScope.moduleTitle = '容器管理 > 容器';
	    $scope.title = "主机详情";
	    $rootScope.link = '/statics/css/common.css';//引入页面样式
	    $scope.showDetail = $scope.isActive = true;
	    $scope.datamodel='static';
	    $scope.resultTip={ //图表提示
	    	cup: false,
	    	net: false,
	    	mem: false,
	    	netOut: false
	    }; 
	    
		/* UI事件绑定
	    ---------------------------------------*/
	    $scope.goBack= goBackFun;
	    $scope.modelChange=modelChangeFun;
	    
		/* 入口函数 （代码执行的唯一入口，也是起始点，位置可以根据具体情况调整）
	    ---------------------------------------*/
	   (function () {
	       _init();
	   })();
	
	   /* UI事件实现
	    ---------------------------------------*/
	   function goBackFun(){
	       if(flag==0){
	           $state.go('app.containerManger.container');//返回容器菜单列表页
	       }else if(flag==1){
	          $state.go('app.containerManger.img');//返回镜像菜单列表页
	       }else if(flag==2){
			   $state.go('app.containerManger.net');//返回镜像菜单列表页
		   }else if(flag==3){
               $state.go('app.application.develop');//返回镜像菜单列表页
           }
	       // $state.go('app.application.developDetail',{id:$stateParams.id});
	   }
	   function modelChangeFun(str){
           var i=0,obj,s=0;
           currentObj.model='change';
           currentObj.type=str;
           _clearInterval();
           cupChart.old=[{data:[]}];
           netChart.old=[{data:[]}];
           netOutChart.old=[{data:[]}];
           memoryChart.old=[{data:[]}];
           if(str==='static'){
               _lineChar(currentObj);
           }else{
			   nowtime=0;
               for(i =-60; i < 0;i++){
               	obj=_beforerandomData(i);
               	   netChart.old[0].data.push(obj);
               	   netOutChart.old[0].data.push(obj);
                   cupChart.old[0].data.push(obj);
                   memoryChart.old[0].data.push(obj);
               }
               _getDataCicle();
               window.timer=$interval(_getDataCicle,5000);
           }
       }
	   /* 内部方法实现
	   ---------------------------------------*/
	    function _init(){
	    	var conName = $stateParams.conName;
	    	_getContDeInfo(conName);
	    }
	    //获取容器的详细信息
	    function _getContDeInfo(conName){
	    	httpLoad.loadData({
	            url:'/deployer/getConatinerDetailInfo.do',
	            data: {instanceRename: conName},
	            noParam: true,
	            success:function(data){
	                $scope.conInstanceCore = data.instanceCore;
	                $scope.conInstanceEnvsList = data.instanceEnvsList;
	                $scope.conInstancePorts = data.instancePorts;
	                $scope.conInstanceVolumesList = data.instanceVolumesList;
	                $scope.showDetail = $scope.isActive = true;
	                network=[data.instanceCore.instanceNetwork];
	                currentObj={objectName:data.instanceCore.containerName,monitorType:'container'};
	                _lineChar(currentObj);
	            }
	        });
	    }
	    /*线图数据*/
        function _lineChar(obj){
            var resultData;
            httpLoad.loadData({
                url:'/monitor/monitor/history',
                data:{
                    objectName:obj.objectName,
                    monitorType:obj.monitorType
                },
                success:function(data){
                    if(data.success&&data.data){
                    	resultData=data.data;
                    	if(resultData.cpu_used_persent.length<1){
                    		$scope.resultTip.cup=true;
                    	}
                    	if(resultData.packets_received.length<1){
                    		$scope.resultTip.net=true;
                    	}
                    	if(resultData.mem_used_persent.length<1){
                    		$scope.resultTip.mem=true;
                    	}
                    	if(resultData.packets_sent.length<1){
                    		$scope.resultTip.netOut=true;
                    	}
                        if(currentObj.model==='change'){
                            cupChart.old=eChartLineFactory.changData({type:currentObj.type,data:[resultData.cpu_used_persent] });
                            netOutChart.old=eChartLineFactory.changData({type:currentObj.type,data:[resultData.packets_sent],model:'net'});
                            netChart.old=eChartLineFactory.changData({type:currentObj.type,data:[resultData.packets_received],model:'net'});
                            memoryChart.old=eChartLineFactory.changData({type:currentObj.type,data:[resultData.mem_used_persent]});
                            _changeChart(cupChart);
                            _changeChart(netChart);
                            _changeChart(netOutChart );
                            _changeChart(memoryChart);
                        }else{

                            cupChart.Config=eChartLineFactory.setChartConfig({title:"CPU",data:[eChartLineFactory.initData(resultData.cpu_used_persent)],seriesNames:['CPU']});
                            cupChart.id="img_cpu";
                            cupChart.type="static";
                            _drawChart(cupChart);

                            netChart.Config=eChartLineFactory.setChartConfig({title:"入网",data:[eChartLineFactory.initData(resultData.packets_received,'net')],seriesNames:['入网']});
                            netChart.id="img_net";
                            netChart.type="static";
                            _drawChart(netChart);

                            netOutChart.Config=eChartLineFactory.setChartConfig({title:"出网",data:[eChartLineFactory.initData(resultData.packets_sent,'net')],seriesNames:['出网']});
                            netOutChart.id="img_net_out";
                            netOutChart.type="static";
                            _drawChart(netOutChart);

                            memoryChart.Config=eChartLineFactory.setChartConfig({title:"内存",data:[eChartLineFactory.initData(resultData.mem_used_persent)],seriesNames:['内存']});
                            memoryChart.id="img_memery";
                            memoryChart.type="static";
                            _drawChart(memoryChart);

                        }
                    }else{
                    	$scope.resultTip={
            		    	cup: true,
            		    	net: true,
            		    	mem: true,
            		    	netOut: true
            		    };
                    }
                }
            });
        }
	    //补0数据
	    function _beforerandomData(i) {
	        nowtime = new Date(+ctime + oneDay*i).getTime();
	        return {
	            name:nowtime,
	            value: [
	                nowtime,
	                0
	            ]
	        };
	    }
	    function _clearInterval(){
            if(window.timer){
                $interval.cancel(window.timer);
            }
            delete window.timer;
        }
	    function _drawChart(chartConf){
            var myChart = echarts.init(document.getElementById(chartConf.id));
            chartConf.myChart=myChart;
            myChart.setOption(chartConf.Config);
        }
        function _changeChart(chartConf){
        	var i=1,series=[
    	            { data:angular.copy(chartConf.old[0].data) }
        	 ];
        	if(chartConf.old.length>1){
        		for(i=1;i<chartConf.old.length;i++){
        			series.push( { data:angular.copy(chartConf.old[i].data)} );
        		}
        	}
            chartConf.myChart.setOption({
                series:series
            });
        }
        function _getDataCicle(){
        	var resultData;
                httpLoad.loadData({
                    url:'/monitor/monitor/immediate',
                    data:{
                        objectName:currentObj.objectName,
                        monitorType:currentObj.monitorType
                    },
                    success:function(data){
                        if(data.success&&data.data){
                        	resultData=data.data;
                        	if(resultData.cpu_used_persent.length<1){
                        		$scope.resultTip.cup=true;
                        	}else{
                        		cupChart.old=eChartLineFactory.changData({old:cupChart.old,data:[resultData.cpu_used_persent],type:'auto'});
                        		_changeChart(cupChart);
                        		$scope.resultTip.cup=false;
                        	}
                        	if(resultData.packets_received.length<1){
                        		$scope.resultTip.net=true;
                        	}else{
                        		netChart.old=eChartLineFactory.changData({old:netChart.old,data:[resultData.packets_received],type:'auto',model:'net'});
                        		_changeChart(netChart);
                        		$scope.resultTip.net=false;
                        	}
                        	if(resultData.mem_used_persent.length<1){
                        		$scope.resultTip.mem=true;
                        	}else{
                        		memoryChart.old=eChartLineFactory.changData({old:memoryChart.old,data:[resultData.mem_used_persent],type:'auto'});
                        		_changeChart(memoryChart);
                        		$scope.resultTip.mem=false;
                        	}
                        	if(resultData.packets_sent.length<1){
                        		$scope.resultTip.netOut=true;
                        	}else{
                        		netOutChart.old=eChartLineFactory.changData({old:netOutChart.old,data:[resultData.packets_sent],type:'auto',model:'net'});
                        		_changeChart(netOutChart);
                        		$scope.resultTip.netOut=false;
                        	}
                        }else{
                            _clearInterval();
                            $scope.resultTip={
                		    	cup: true,
                		    	net: true,
                		    	mem: true,
                		    	netOut: true
                		    };
                        }
                    }
                });
         }
    }]);
})();
